package com.ywb.controller;

import com.ywb.anno.Log;
import com.ywb.pojo.Dept;
import com.ywb.pojo.Result;
import com.ywb.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list(){
//        System.out.println("查询全部部门数据");
        log.info("查询全部部门数据");
        List<Dept> depts = deptService.findAll();
        return Result.success(depts);
    }
    /*方法一 httprequest*/

//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String ids=request.getParameter("id");
//        int id = Integer.parseInt(ids);
//        System.out.println("删除部门"+id);
//        return Result.success();
//    }
    /*方法二*/
//
/*方式三 如果形参名与传递的请求参数名一样可以省略RequestParam*/
    @Log
    @DeleteMapping
    public Result delete(Integer id){
//        System.out.println("删除部门"+id);
        log.info("删除部门{}",id);
        deptService.deleteById(id);
        return Result.success();
    }
    /*新增部门*/
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
//        System.out.println("新增部门"+dept);
        log.info("新增部门{}",dept);
        deptService.add(dept);
        return Result.success();
    }

//    根据id查询数据 通过pathvariable指定形参绑定 但如果形参名id与传递的请求参数名一致 则可以省略pathvariable
    @GetMapping("/{id}")
    public Result getinfo(@PathVariable("id")Integer deptid){
//        System.out.println("根据id查询数据"+deptid);
        log.info("根据id查询数据{}",deptid);
        Dept dept = deptService.getById(deptid);
        return Result.success(dept);
    }
    @PutMapping
    @Log
    public Result update(@RequestBody Dept dept)
    {
//        System.out.println("修改部门"+dept);
        log.info("修改部门{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
